use v5.12;
use FindBin;
#==============================================================================
#
# Name: Main-Script.pl
#
# Written by: Samuel Barton
#
# Date: February - May 2014
#
# Project: Manager Scripts
#
# Description:
#
# This script will consist of an infinite loop which does the following each 
# iteration:
#     1. Call script to execute lsusb and place what it returns in a text file.
#         - the text file will be called lsusb.txt (the path will be determined
#           before runtime and saved as a constant in this script)
#
#     2. Loop through all connected devices and run subscripts to log data for
#        each supported device.
#         - each subscript returns the path to its log file.
#
#     3. Call transmit script passing it the list of paths to the log files.
#         - transmit script returns status.
#
#==============================================================================

#    - path to list of currently connected devices.
my $CONNECTED_DEVICES_PATH = "DEVICES.txt";
#    - time to sleep at the end of each iteration of the loop
my $SLEEP_TIME = 60;
#    - path to logging subscripts
my $SUBSCRIPT_PATH = "/loging_subscripts/";
#    - path to logs
my $LOG_PATH = "/logs/";
#    - variable to stop the loop
my $EXIT = 0;
#    - date format used in softlinks management
my $date_format = "%H%M%S";

# This variable tells whether or not the directory has been cleaned out today
my $cleanout_status = 0; # 0 for not cleaned out, 1 for cleaned out

# This is the "infinite" loop which starts all the logging scripts
until ($EXIT)
{
    # TEMPORARY ---------------------------------------------------------------
    $EXIT = 1;
    # -------------------------------------------------------------------------

    # If the time is in minute of 12:00 am (between 12:00:00 and 12:00:59)
    # then set the cleanout status to 0 so that the softlinks direcroty will be
    # cleaned out.
    my $date = `date +$date_format`;
    chomp($date);
    
    if ($date < 60)
    {
        # clean out softlinks subdirectory of logs
        system("rm $FindBin::Bin/logs/current/*");
    }


    # Call lsusb
    system("perl lsusb.pl"); # no status yet, figure out if one makes sense.

    # Run subscripts for each connected device which is supported
    # open the file containing the connected devices
    open(connected_devices, "<", 
         $CONNECTED_DEVICES_PATH) or die "couldn't open file: $!";

    # loop through the connected devices file and start subscripts for each
    # supported device currently connected.
    for my $connected_dev (<connected_devices>)
    {
        # remove line termination character to enable equality matching
        chomp($connected_dev);

        # attempt to run a subscript for the file

        # Change to qx and have it return an exit status
        my $status = qx("perl $connected_dev.pl");

        # If the script had an error (device disconeccted, unsupported device,
        # &etc), then log that it did to an error log. Otherwise simply 
        # continue execution
    }

    # Close connection to the list of currently connected devices
    close(connected_devices);

    # Call transmit script

    # Wait <DETERMINE HOW LONG> before running the loop again.
    # sleep (SLEEP_TIME);
}
